﻿using System;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Webdiver;
namespace AnyOffice.general.sms
{
    public partial class send : ModulePage
    {
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            SmsDao dao = new SmsDao();
            foreach (RepeaterItem item in this.rptSms.Items)
            {
                HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("SmsId");
                if ((box != null) && box.Checked)
                {
                    dao.Delete(base.Conn, int.Parse(box.Value), base.User.Identity.Name);
                }
            }
            DataTable table = dao.Send(base.Conn, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, true, base.User.Identity.Name);
            if (int.Parse(table.Rows[0][0].ToString()) == 0)
            {
                this.litNavCount.Visible = false;
                this.litNavPager.Visible = false;
                this.btnDeleteAll.Visible = false;
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowMessage("无已发送的短信息");
            }
            else
            {
                this.AspNetPager1.RecordCount = int.Parse(table.Rows[0][0].ToString());
                this.BindData();
            }
        }
        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
            new SmsDao().DeleteAll(base.Conn, base.User.Identity.Name, SmsDao.DeleteType.Send);
            this.litNavCount.Visible = false;
            this.litNavPager.Visible = false;
            this.btnDeleteAll.Visible = false;
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowMessage("无已发送的短信息");
        }
        public void ChangePage(object src, PageChangedEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.BindData();
        }
        public string formatContent(string strContent)
        {
            strContent = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.IgnoreCase).Replace(strContent, "<a href=$1 target=_blank>$1</a>");
            strContent = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.IgnoreCase).Replace(strContent, "<a href=mailto:$1>$1</a>");
            return strContent.Replace("\n", "<br>");
        }
        private void BindData()
        {
            DataTable table = new SmsDao().Send(base.Conn, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, false, base.User.Identity.Name);
            this.rptSms.DataSource = table;
            this.rptSms.DataBind();
            this.litNavCount.Text = "<span class=\"small1\">共&nbsp;<span class=\"big4\">" + this.AspNetPager1.RecordCount.ToString() + "</span>&nbsp;条";
            this.litNavPager.Text = "<span class=\"small1\">当前为第" + this.AspNetPager1.CurrentPageIndex.ToString() + "页，共" + this.AspNetPager1.PageCount.ToString() + "页，每页最多" + this.AspNetPager1.PageSize.ToString() + "条</span>";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.AspNetPager1.PageSize = new ConfigManager().PageSize;
                DataTable table = new SmsDao().Send(base.Conn, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, true, base.User.Identity.Name);
                if (int.Parse(table.Rows[0][0].ToString()) == 0)
                {
                    this.litNavCount.Visible = false;
                    this.litNavPager.Visible = false;
                    this.btnDeleteAll.Visible = false;
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowMessage("无已发送的短信息");
                }
                else
                {
                    this.AspNetPager1.RecordCount = int.Parse(table.Rows[0][0].ToString());
                    this.BindData();
                    this.btnDeleteAll.Attributes.Add("onclick", "return delete_all()");
                    this.btnDelete.Attributes.Add("onclick", "return delete_sms()");
                }
            }
        } 

    }
}
